package com.example.shop_2302.order

import androidx.lifecycle.viewModelScope
import com.example.base.entity.CreateOrderEntity
import com.example.base.entity.GetPreOrderEntity
import com.example.base.entity.OrderEntity
import com.example.base.entity.PayEntity
import com.example.base.entity.Res
import com.example.base.mvvm.BaseViewModel
import com.example.base.param.ParamCreateOrder
import com.example.base.param.ParamPay
import com.example.shop_2302.utils.SingleLiveData
import kotlinx.coroutines.launch

class OrderViewModel:BaseViewModel() {
    //返回值---获取预下单
    val getSuccess = SingleLiveData<Res<GetPreOrderEntity>>()
    val getFail = SingleLiveData<String>()
    fun getPreOrder(preOrderNo:String){
        viewModelScope.launch {
            try {
                getSuccess.postValue(apiServer.getPreOrder(preOrderNo))
            }catch (e:Exception){
                getFail.postValue(e.message.toString())
            }
        }
    }
    //返回值 --创建订单
    val createSuccess = SingleLiveData<Res<CreateOrderEntity>>()
    val createFail = SingleLiveData<String>()
    fun createOrder(paramCreateOrder: ParamCreateOrder){
        viewModelScope.launch {
            try {
                createSuccess.postValue(apiServer.createOrder(paramCreateOrder))
            }catch (e:Exception){
                createFail.postValue(e.message.toString())
            }
        }
    }

    //返回值---支付
    val paySuccess = SingleLiveData<Res<PayEntity>>()
    val payFail = SingleLiveData<String>()
    fun pay(paramPay: ParamPay){
        viewModelScope.launch {
            try {
                paySuccess.postValue(apiServer.pay(paramPay))
            }catch (e:Exception){
                payFail.postValue(e.message.toString())
            }
        }
    }

    //返回值---订单列表
    val orderSuccess = SingleLiveData<Res<OrderEntity>>()
    val orderFail = SingleLiveData<String>()
    fun order(type:String,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                orderSuccess.postValue(apiServer.order(type, limit, page))
            }catch (e:Exception){
                orderFail.postValue(e.message.toString())
            }
        }
    }
}